#include "WindowsRenderer.h"

using namespace Graphics::Renderer;

WindowsRenderer::WindowsRenderer(HWND window) {
    this->window = window;
    
    // Device
    this->device = GetDC(window);
    
    // Buffer
    CreatePixelFormat();
    
    // Context
    this->renderer = wglCreateContext(device);
    wglMakeCurrent(device, renderer);
}

bool WindowsRenderer::CreatePixelFormat(void) {
    PIXELFORMATDESCRIPTOR pfd;
    memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
    
    pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
    pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 32;
    pfd.cDepthBits = 32;
    pfd.iLayerType = PFD_MAIN_PLANE;
    
    int pixelFormat = ChoosePixelFormat(device, &pfd);
    bool result = SetPixelFormat(device, pixelFormat, &pfd);
    
    return result;
}

WindowsRenderer::~WindowsRenderer(void) {
    wglMakeCurrent(device, 0);
    wglDeleteContext(renderer);
    ReleaseDC(window, device);
}
